﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Bussiness;

public partial class HistoryOptimization : System.Web.UI.Page
{
    private double burden = 0;
    private double temperature = 0;
    private PiLink.declare PIDeclare = null;

    /// <summary>
    /// 页面加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        string water = ConfigurationManager.ConnectionStrings["water"].ToString();
        string fh = ConfigurationManager.ConnectionStrings["FH"].ToString();

        if (!IsPostBack)
        {
            this.calSearchTime.Text = DateTime.Now.ToString();
            BindParaType(ParaMoudle.GKXY1);
        }

        if (PIDeclare == null)
            InitLink();
        PIModel model = new PIModel();
        this.lblBurden.Text = model.GetHistory(fh, this.calSearchTime.Text, PIDeclare).ToString("0.00");
        this.lblTemperature.Text = model.GetHistory(water, this.calSearchTime.Text, PIDeclare).ToString("0.00");

    }

    /// <summary>
    /// 绑定参数类型数据
    /// </summary>
    /// <param name="paraMoudle"></param>
    private void BindParaType(string paraMoudle)
    {
        SysParament para = new SysParament();
        this.ddlParames.DataSource = para.GetSysParaType(paraMoudle);
        this.ddlParames.DataTextField = "PARATYPE";
        this.ddlParames.DataBind();

        this.ddlParames.Items.Insert(0, new ListItem("全部", "0"));
    }
    /// <summary>
    /// 初始化PI数据连接
    /// </summary>
    private void InitLink()
    {
        string server = ConfigurationManager.ConnectionStrings["server"].ToString();
        string piuser = ConfigurationManager.ConnectionStrings["piuser"].ToString();
        string password = ConfigurationManager.ConnectionStrings["password"].ToString();

        DB2Provider.PIBaseLink link = new DB2Provider.PIBaseLink();
        link.ServerName = server;
        link.UserName = piuser;
        link.PassWord = password;

        PIDeclare = link.GetPI();
    }
    /// <summary>
    /// 绑定历史工况数据
    /// </summary>
    private void Bind()
    {
        if (PIDeclare == null)
            InitLink();

        // 获取参数编号集
        SysParament para = new SysParament();
        DataTable dt = para.GetSysPara(this.ddlParames.Text);
        string paraTypeCollection = "";
        if (dt != null && dt.Rows.Count > 0)
        {
            foreach (DataRow dr in dt.Rows)
            {
                paraTypeCollection += "'" + dr["PARAID"].ToString() + "',";
            }
        }
        paraTypeCollection = paraTypeCollection.Substring(0, paraTypeCollection.LastIndexOf(","));

        if (this.ddlCompute.Text == "2")
        {
            string water = ConfigurationManager.ConnectionStrings["water2"].ToString();
            string fh = ConfigurationManager.ConnectionStrings["FH2"].ToString();
            PIModel model = new PIModel();
            this.lblBurden.Text = model.GetHistory(fh, this.calSearchTime.Text, PIDeclare).ToString("0.00");
            this.lblTemperature.Text = model.GetHistory(water, this.calSearchTime.Text, PIDeclare).ToString("0.00");
        }

        Optimization C_Optimization = new Optimization();
        burden = Convert.ToDouble(this.lblBurden.Text);
        temperature = Convert.ToDouble(this.lblTemperature.Text);
        dt = C_Optimization.GetHistoryOptimization(burden, temperature, paraTypeCollection, this.calSearchTime.Text, PIDeclare, this.ddlCompute.Text);
        this.grvFeeInfo.DataSource=dt;
        this.grvFeeInfo.DataBind();
        
    }
    /// <summary>
    /// 查询历史工况
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Bind();
    }
    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void grvFeeInfo_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.grvFeeInfo.PageIndex = e.NewPageIndex;
        Bind();
        
    }
}
